package cas.ihep.fs.hdfs;

import org.apache.hadoop.hdfs.DFSClient;

import java.lang.reflect.Field;

class DFSClientConfAdapter {

    static int socketTimeout(DFSClient.Conf cnf){
        try {
            return _socketTimeout.getInt(cnf);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    static boolean connectToDnViaHostname(DFSClient.Conf cnf){
        try {
            return _connectToDnViaHostname.getBoolean(cnf);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    static int nBlockWriteLocateFollowingRetry(DFSClient.Conf cnf){
        try {
            return _nBlockWriteLocateFollowingRetry.getInt(cnf);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    static int hdfsTimeout(DFSClient.Conf cnf){
        try {
            return _hdfsTimeout.getInt(cnf);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    private static final Field _socketTimeout,_connectToDnViaHostname,_nBlockWriteLocateFollowingRetry,_hdfsTimeout;

    static{
        Class<DFSClient.Conf> klass=DFSClient.Conf.class;

        try {
            _socketTimeout = klass.getDeclaredField("socketTimeout");
            _connectToDnViaHostname = klass.getDeclaredField("connectToDnViaHostname");
            _socketTimeout.setAccessible(true);
            _connectToDnViaHostname.setAccessible(true);
            _nBlockWriteLocateFollowingRetry=klass.getDeclaredField("nBlockWriteLocateFollowingRetry");
            _nBlockWriteLocateFollowingRetry.setAccessible(true);
            _hdfsTimeout=klass.getDeclaredField("hdfsTimeout");
            _hdfsTimeout.setAccessible(true);
        }catch (NoSuchFieldException e){
            throw new RuntimeException(e);
        }
    }
}
